package com.yami.shop.seckill.common.listener;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.bo.ProductBO;
import com.yami.shop.bean.enums.ProdType;
import com.yami.shop.bean.event.EsProductEvent;
import com.yami.shop.bean.order.EsProductOrder;
import com.yami.shop.seckill.common.dao.SeckillMapper;
import com.yami.shop.seckill.common.dao.SeckillSkuMapper;
import com.yami.shop.seckill.common.model.Seckill;
import com.yami.shop.seckill.common.model.SeckillSku;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 获取es商品团购数据事件
 * @author yami
 */
@Component("seckillEsProductListener")
@AllArgsConstructor
public class EsProductListener {

    private final SeckillMapper seckillMapper;
    private final SeckillSkuMapper seckillSkuMapper;

    @EventListener(EsProductEvent.class)
    @Order(EsProductOrder.SECKILL)
    public void seckillEsProductListener(EsProductEvent event) {
        List<ProductBO> productList = event.getProductList();
        if (CollUtil.isEmpty(productList) || Objects.nonNull(event.getOperationType())) {
            return;
        }
        List<ProductBO> seckillProductList = new ArrayList<>();
        List<Long> seckillIds = new ArrayList<>();
        for (ProductBO productBO : productList) {
            if (!Objects.equals(productBO.getProdType(), ProdType.PROD_TYPE_SECKILL.value())) {
                continue;
            }
            seckillProductList.add(productBO);
            seckillIds.add(productBO.getActivityId());
        }
        if (CollUtil.isEmpty(seckillIds)) {
            return;
        }
        // 获取已启用的秒杀商品信息
        List<Seckill> seckillList = seckillMapper.selectList(new LambdaQueryWrapper<Seckill>().in(Seckill::getSeckillId, seckillIds).eq(Seckill::getStatus, 1));
        Map<Long, Seckill> seckillMap = seckillList.stream().collect(Collectors.toMap(Seckill::getSeckillId, s -> s));
        List<SeckillSku> seckillSkus = seckillSkuMapper.listSeckillMinOriginalPrice(seckillIds);
        Map<Long, Double> seckillSkuMap = seckillSkus.stream().collect(Collectors.toMap(SeckillSku::getSeckillId, SeckillSku::getSeckillPrice));
        Long currentTime = System.currentTimeMillis();
        for (ProductBO productBO : seckillProductList) {
            if (!seckillMap.containsKey(productBO.getActivityId())) {
                continue;
            }
            Seckill seckill = seckillMap.get(productBO.getActivityId());
            // 当前时间大于等于活动结束时间， 则不添加活动信息
            if (seckill.getEndTime().getTime() <= currentTime) {
                continue;
            }
            // 插入活动开始时间
            productBO.setActivityStartTime(seckill.getStartTime().getTime());
            // 秒杀最低价
            productBO.setActivityPrice(seckill.getSeckillPrice());
            // 秒杀商品规格中最低的sku原价
            productBO.setActivityOriginalPrice(seckillSkuMap.get(productBO.getActivityId()));
        }
    }

}
